<?php
/**
 * Created by PhpStorm.
 * User: WX-C
 * Date: 2018/4/18
 * Time: 11:34
 */

namespace app\common\model;

use app\common\traits\Base;
use think\Db;
use think\Model;
use think\Request;

class Census extends Model
{
    public function getDataAttr($value){
        return explode(',',$value);
    }
    public function index(){
        $request=Request::instance();
        $limit=$request->input('limit',10,'intval');
        $page=$request->input('page',0,'intval');
        $census=$this->page($page,$limit)
            ->where(['del'=>'0'])
            ->select();
        foreach($census as &$v){
           $uid= Db::table('user')->where(['id'=>$v['uid']])->field('username')->find();
            $v['uid']=$uid['username'];
            $qid=Db::table('questionnaire')->where(['id'=>$v['qid']])->field('name')->find();
            $v['qid']=$qid['name'];
            $wid=Db::table('question')->where(['id'=>$v['wid']])->field('name')->find();
            $v['wid']=$wid['name'];
        }
        return $census;
    }
    public function question(){
        $model=new Question();
        $questions=$model->where(['id'=>['in',$this->data]])
            ->select();
        return $questions;
    }
    public function add($data){
        return $this->allowField(true)
            ->isUpdate(false)
            ->save($data);
    }
    function edit($data){
        return $this->allowField(true)
            ->isUpdate(true)
            ->save($data);
    }

    public function idFind($id){
        return $lists=$this->find($id);
    }

    public function del($id){
        return $lists=$this->where(['id'=>$id])->update(['del'=>1]);
    }
}